package com.gitee.neuray.security.modules.param.controller;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.neuray.security.common.validator.ValidatorUtils;
import com.gitee.neuray.security.modules.param.entity.ParamItemEntity;
import com.gitee.neuray.security.modules.param.service.EngineParamService;
import com.gitee.neuray.security.modules.param.service.ParamItemService;
import com.gitee.neuray.security.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gitee.neuray.security.modules.param.entity.EngineItemEntity;
import com.gitee.neuray.security.modules.param.service.EngineManageService;
import com.gitee.neuray.security.common.utils.PageUtils;
import com.gitee.neuray.security.common.utils.R;



/**
 * 发动机表
 *
 * @author neuray
 * @email neuray@neuray.com
 * @date 2020-09-17 09:59:45
 */
@RestController
@RequestMapping("engine/manage")
public class EngineManageController {
    @Autowired
    private EngineManageService engineManageService;
    @Autowired
    private ParamItemService paramItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("engine:manage:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = engineManageService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 不分页列表
     */
    @RequestMapping("/listnopage")
    @RequiresPermissions("engine:manage:listnopage")
    public R listnopage(@RequestParam Map<String, Object> params){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id",ShiroUtils.getUserEntity().getDeptId());
        List<EngineItemEntity> list = engineManageService.list(queryWrapper);

        return R.ok().put("page", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("engine:manage:info")
    public R info(@PathVariable("id") String id){
        EngineItemEntity engineItem = engineManageService.getById(id);
        List<ParamItemEntity> params = paramItemService.getParamByEngine(id,ShiroUtils.getUserEntity().getDeptId());
        List<Long> ids = new ArrayList<>();
        params.stream().forEach((param) -> {
            ids.add(param.getId());
        });
        engineItem.setParamIdList(ids);
        return R.ok().put("engineItem", engineItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("engine:manage:save")
    public R save(@RequestBody EngineItemEntity engineItem){

        engineManageService.saveEngine(engineItem);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("engine:manage:update")
    public R update(@RequestBody EngineItemEntity engineItem){
        //ValidatorUtils.validateEntity(engineItem);
        engineManageService.updateEngine(engineItem);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("engine:manage:delete")
    public R delete(@RequestBody Map map){
        //id
        List<String> ids = (List<String>) map.get("ids");
        //表名
        List<String> list = (List<String>) map.get("obj");;
        engineManageService.deleteEngine(ids,list);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/getParam")
    @RequiresPermissions("engine:manage:list")
    public R getParam(){

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id",ShiroUtils.getUserEntity().getDeptId());
        List<ParamItemEntity> ret = paramItemService.list(queryWrapper);
        return R.ok().put("paramList", ret);
    }


}
